﻿function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}


//回车键触发登录请求
document.onkeydown = function (e) {
    if (!e) e = window.event; //火狐中是 window.event
    if ((e.keyCode || e.which) == 13) {
        login();
    }
};

$(function () {
    $('#enterbtn').bind('click', function () { login(); });
});
function login() {
    var userID = $('#userId').val();
    var pwd = $('#password').val();
    if (userID == '') {
        alert('Login name cannot be empty!');
        return;
    }
    if (pwd == '') {
        alert('Password name cannot be empty!');
        return;
    }
    $.ajax({
        url: App_Config.TadongUrl + "/LoginAuth.aspx",
        type: 'post',
        dataType: 'json',
        data: { "data": $.toJSON({ 'name': userID, 'pwd': hex_md5(pwd) }) },
        success: function (data) {
            if (data.State == 0 && data.Data && data.Data.OEMSession) {

                setCookie('_userId_', data.Data.USER_ID, 1);
                setCookie('_userAccount_', data.Data.USER_ACCOUNT, 1);
                //setCookie('_language_', data.Data.USER_LANG);
                $.cookie('_language_', data.Data.USER_LANG);
                $.cookieSet(data.Data.OEMSession);

                $.ajax({
                    url: App_Config.CurrentUrl + "/handlers/LoginAuth.ashx",
                    type: 'post',
                    dataType: 'json',
                    data: { "data": $.toJSON({ 'name': userID, 'pwd': hex_md5(pwd) }) },
                    success: function (data) {
                        if (data.State == 0 && data.Data && data.Data.OEMSession) {

                            location.href = "Views/DataSearch/SerchHead.aspx";
                        } else {
                            alert(data.ErrMsg);
                        }
                    }
                });

                //location.href = "index.html";
            } else {
                alert(data.ErrMsg);
            }
        }
    });
   

    
}